Page({
    data: {
        showAllSelect: false, //是否全选（底部结算全选按钮）
        totalPrice: 0, //合计总价
        // 购物车列表
        list: [],
        listLoading: false,
        goodsid: '',
        num: 1,
        czList: [],
        showConfirmDel: false,
        deletArr: [],
        winHeight: 0,
    },

    onLoad(options) {
        const systemInfo = wx.getSystemInfoSync();
        this.setData({
            showAllSelect: false,
            totalPrice: 0,
            winHeight: systemInfo.statusBarHeight + systemInfo.screenHeight - systemInfo.windowHeight,
        });
    },
    onShow() {
        const systemInfo = wx.getSystemInfoSync();
        this.setData({
            showAllSelect: false,
            totalPrice: 0,
            winHeight: systemInfo.statusBarHeight + systemInfo.screenHeight - systemInfo.windowHeight,
        });
        this.paging.reRequest();
    },

    // 步进器变化触发事件
    onNumChange(e) {
        console.log(e);
        const id = e.currentTarget.dataset.id;
        let that = this;
        const index = e.currentTarget.dataset.index;
        const stepNum = e.detail;
        const list = that.data.list;
        list[index].num = stepNum;
        that.setData({
            list: that.data.list,
        });
        // 修改购物车数量
        this.request({
            url: 'user/goodsCar/update',
            method: 'POST',
            data: {
                car_id: id,
                num: stepNum,
            },
        }).then(res => {});

        this.computedTotalPirce();
    },
    // 点击商品item项(小选控制全选)
    onGoodItem(e) {
        // console.log(e);
        let index = e.currentTarget.dataset.index;
        let list = this.data.list;
        this.data.showAllSelect = false;
        list[index].selected = !list[index].selected;

        // 商品的每一项都选中了
        const isAll = list.every(item => {
            return item.selected === true;
        });

        this.setData({
            list: list,
            showAllSelect: isAll, //底部全选框设置为全选状态
        });
        this.computedTotalPirce();
    },
    // 点击全选按钮(全选控制小选)
    onAllSeleted() {
        let list = this.data.list;
        if (list.length === 0) {
            return wx.showToast({
                title: '还没有商品哦',
                icon: 'none',
            });
        }
        for (let i = 0; i < list.length; i++) {
            list[i].selected = !this.data.showAllSelect;
        }
        this.setData({
            showAllSelect: !this.data.showAllSelect,
            list: list,
        });
        this.computedTotalPirce();
    },

    // 左滑删除购物车
    onDeleteLeft(e) {
        let id = e.currentTarget.dataset.id;
        this.request({
            url: 'user/goodsCar/destroy',
            method: 'POST',
            data: {
                car_ids: [id],
            },
        }).then(res => {
            console.log(res);
            // 提示成功
            wx.showToast({
                title: res.msg,
            });
            // 刷新列表
            this.paging.reRequest();
            // 重新计算价格
            this.computedTotalPirce();
        });
    },

    // 计算总价的函数
    computedTotalPirce() {
        let list = this.data.list;
        let total = 0;
        for (let i = 0; i < list.length; i++) {
            if (list[i].selected) {
                total += list[i].num * list[i].total_price;
            }
        }
        this.setData({
            list: list,
            totalPrice: total.toFixed(2),
        });
    },
    // 去结算
    toSettlement() {
        const { list } = this.data;
        let newArr = list.filter(item => {
            return item.selected;
        });

        let someArr = list.some(item => {
            return item.selected;
        });

        if (someArr == false)
            return wx.showToast({
                title: '请选择商品哦',
                icon: 'none',
            });

        wx.navigateTo({
            url: '/pages/cartSettlement/cartSettlement',
            success: function (res) {
                // 通过eventChannel向被打开页面传送数据
                res.eventChannel.emit('acceptDataFromOpenerPage', { data: newArr });
            },
        });
    },
    // 右上角购物车删除按钮(可多选删除)
    delMultiplechoice() {
        const { list } = this.data;
        const someDelArr = list.some(item => item.selected);
        if (!someDelArr)
            return wx.showToast({
                title: '请选择需要删除的商品',
                icon: 'none',
            });

        let delList = list.filter(item => {
            return item.selected;
        });
        // 处理数据传给后端
        let deletArr = delList.map(item => {
            return {
                id: item.goods_id,
            };
        });
        this.setData({
            deletArr,
            showConfirmDel: true,
        });
    },

    // 确认删除
    onDelete() {
        wx.showLoading({
            title: '删除中',
        });
        this.request({
            url: 'user/goodsCar/destroy',
            method: 'POST',
            data: {
                car_ids: this.data.deletArr,
            },
        })
            .then(res => {
                // 提示成功
                wx.showToast({
                    title: res.msg,
                });
                this.setData({
                    showConfirmDel: false,
                });
                // 刷新列表
                this.paging.reRequest();
            })
            .catch(err => {
                this.setData({
                    showConfirmDel: false,
                });
                wx.hideLoading();
            });
    },

    closeDialog() {
        this.setData({
            showConfirmDel: false,
        });
    },
});
